<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery文件上传</title>
    <script src="lib/jquery.js"></script>
</head>
<body>
<input type="file" name="" id="files">
<br>
<button id="btnFile">上传文件</button>
<script>
    $('#btnFile').on('click', function () {
        var file = $('#files')[0].files
        if (file.length <= 0) {
            return alert('请选择你需要上传的文件')
        }
        var fd = new FormData()
        fd.append('avatar', file[0])
        $.ajax({
            method: 'POST',
            url: 'http://www.liulongbin.top:3006/api/upload/avatar',
            data: fd,
            // 不对发送的数据进行编码
            processData: false,
            contentType: false,
            success: function (res) {
                console.log(res);
            }
        })
    })

</script>
</body>
</html>
